/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
package view;

import Model.Aluno;
import Model.AlunoDAO;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;

/**
 *
 * @author GIOVANI
 */
public class TelaAlunoConsulta extends VBox {
    
    private AlunoDAO dao = new AlunoDAO();

    public TelaAlunoConsulta(final AlunoAba parent) {
        HBox hTable = new HBox();
        VBox vBotoes = new VBox();
        Button add = new Button("Adicionar");
        Button edit = new Button("Editar");
        Button remove = new Button("Remover");
               
            final TableView tabela = new TableView();
            
            
            TableColumn matricula = new TableColumn();
                matricula.setText("Matricula");
                matricula.setCellValueFactory(new PropertyValueFactory<Aluno, Integer>("matricula"));
            TableColumn nome = new TableColumn();
                nome.setText("Nome");
                nome.setCellValueFactory(new PropertyValueFactory<Aluno, String>("nome"));
            TableColumn idade = new TableColumn();
                idade.setText("Idade");           
                idade.setCellValueFactory(new PropertyValueFactory<Aluno, Integer>("idade"));
            TableColumn sexo = new TableColumn();
                sexo.setText("Sexo");
                sexo.setCellValueFactory(new PropertyValueFactory<Aluno, String>("sexo"));
            TableColumn cpf = new TableColumn();
                cpf.setText("CPF");           
                cpf.setCellValueFactory(new PropertyValueFactory<Aluno, String>("cpf"));
            TableColumn cep = new TableColumn();
                cep.setText("CEP");
                cep.setCellValueFactory(new PropertyValueFactory<Aluno, String>("cep"));
            TableColumn bairro = new TableColumn();
                bairro.setText("Bairro");
                bairro.setCellValueFactory(new PropertyValueFactory<Aluno, String>("bairro"));
            
                
            final ObservableList<Aluno> data = FXCollections.observableArrayList(dao.findAll());
            
                    
            tabela.getColumns().addAll(matricula, nome, idade, sexo, cpf, cep, bairro);
            tabela.setMinWidth(560);
            tabela.setItems(data);
       
       //CRIA LOGO        
       //String url = "src/images/LogoConsulta.png";
       ImageView logo = new ImageView( new Image(getClass().getResourceAsStream("../images/LogoConsulta.png"), 170, 120, false, true));
        //FIM LOGO
            
            edit.setMinWidth(70);
            remove.setMinWidth(70);
            
            vBotoes.getChildren().addAll(edit, remove, logo);
            vBotoes.setSpacing(15);
        
        Label consulta = new Label("Consulta de Alunos");
        consulta.setFont( new Font( "Arial", 20));
     
        
        hTable.getChildren().addAll( tabela, vBotoes);
            hTable.setSpacing(15);
            hTable.setMaxSize(700, 450);
            
       //EVENTOS
       edit.setOnAction(new EventHandler<ActionEvent>() {

                @Override
                public void handle(ActionEvent t) {
                    Aluno aluno = (Aluno) tabela.getSelectionModel().getSelectedItem();
                    parent.montaCentro_formulario_edicao(aluno);
            }
            });
       remove.setOnAction(new EventHandler<ActionEvent>() {

                @Override
                public void handle(ActionEvent t) {
                Aluno aluno = (Aluno) tabela.getSelectionModel().getSelectedItem();
                
                dao.remove(aluno);
                data.clear();
                data.addAll(dao.findAll());
                
            }
            });
       edit.setOnAction(new EventHandler<ActionEvent>(){

            @Override
            public void handle(ActionEvent t) {
                tabela.setEditable(true);
                
            }
                
                
                
                
                
       });
       //FIM EVENTOS
        
        this.getChildren().addAll( consulta, hTable );
            this.setTranslateX(10);
            this.setTranslateY(10);
            this.setSpacing(15);
        //return vAll;
    }

}
